<?php
function api_sign($postData, $key)
{
    ksort($postData);
    reset($postData);
    $data = sign_implode($postData);
    $data .= "&" . $key;

    return strtoupper(md5($data));
}

function sign_implode(array $assoc, $inGlue = '=', $outGlue = '&')
{
    $return = '';
    foreach ($assoc as $name => $value) {
        $return .= $name . $inGlue . (string)$value . $outGlue;
    }

    return substr($return, 0, -strlen($outGlue));
}

function setAppUid($act_id, $uid, $host, $port, $db = null, $auth = null)
{
    $redis = new redis();
    $ret = $redis->connect($host, $port);
    if (!$ret) {
        return ['code' => 10001, 'message' => 'connect redis fail'];
    }
    if (!is_null($auth)) {
        $res = $redis->auth($auth);
        if (!$res) {
            return ['code' => 10002, 'message' => 'auth error'];
        }
    }
    if (!is_null($db)) {
        $res = $redis->select($db);
        if (!$res) {
            return ['code' => 10003, 'message' => 'select db not exists'];
        }
    }
    $vote_token = makeToken($act_id, $uid);
    $ret = $redis->hset('app:uid:'.$act_id, $uid, $vote_token);
    if ($ret == 1 || $ret == 0) {
        return ['code' => 200, 'message' => 'set success','vote_token' => $vote_token];
    }

    return ['code' => 10004, 'message' => 'set fail'];
}

function getMillisecond() {
    list($s1, $s2) = explode(' ', microtime());
    return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 10000);
}

function makeToken($act_id, $uid) {
    $time = getMillisecond();
    $str = $act_id.$uid.$time.rand(1,10000);
    return md5($str);
}

function getEnvConfig($envConfigField)
{
    $envConfig = [];

    if (($handle = fopen(__DIR__ . '/../../../../.env', "r")) !== false) {
        while (($data = fgetcsv($handle, 1000, "=")) !== false) {
            if (in_array($data[0], $envConfigField)) {
                $envConfig[$data[0]] = $data[1];
            }
        }
        fclose($handle);
    }

    return $envConfig;
}

header('Content-type: application/json');
$envConfigField = ['SIGN_SECRET', 'REDIS_HOST', 'REDIS_PASSWORD', 'REDIS_PORT', 'REDIS_DB'];
$getParams = $_GET;
$paramError = json_encode([
    'code'    => 10005,
    'message' => 'param error',
]);

if (!isset($getParams['uid']) || empty($getParams['mfSign'])) {
    echo $paramError;
    exit;
}
if (!isset($getParams['mfSign']) || empty($getParams['mfSign'])) {
    echo $paramError;
    exit;
}
$envConfig = getEnvConfig($envConfigField);

$key = isset($envConfig['SIGN_SECRET']) ? $envConfig['SIGN_SECRET'] : null;

$redisHost = isset($envConfig['REDIS_HOST']) ? $envConfig['REDIS_HOST'] : null;
$redisPort = isset($envConfig['REDIS_PORT']) ? $envConfig['REDIS_PORT'] : null;
$redisAuth = isset($envConfig['REDIS_PASSWORD']) ? $envConfig['REDIS_PASSWORD'] : null;
$redisDB = isset($envConfig['REDIS_DB']) ? $envConfig['REDIS_DB'] : null;


$sign = $getParams['mfSign'];
$uid = $getParams['uid'];
$act_id = isset($getParams['act_id']) ? $getParams['act_id'] : '';
unset($getParams['mfSign']);
$newSign = api_sign($getParams, $key);

if ($newSign == $sign) {
    $ret = setAppUid($act_id, $uid, $redisHost, $redisPort, $redisDB, $redisAuth);
    if ($ret['code'] == 200) {
        echo json_encode([
            'code'    => 200,
            'message' => 'success',
            'vote_token' => $ret['vote_token'],
        ]);
    } else {
        echo json_encode([
            'code'    => $ret['code'],
            'message' => $ret['message'],
        ]);
    }
} else {
    echo json_encode([
        'code'    => 10000,
        'message' => 'fail',
    ]);
}